package com.nfIt.tho1;

/**
 * ClossName:MyThreand
 * Pakage:com.nfIt.tho1
 * Description
 *
 * @Author L
 * @Create 2024/5/12 20:11
 * @Version 2023.1.3
 */
public class MyThreand implements Runnable{

    /**
     * 100张票
     */
    private Integer ticket = 100;

    @Override
    public void run() {

       while (true){
           if (ticket <= 0) {
               return;
           }
           getTicket();
       }
    }

    private synchronized void getTicket(){
        if (ticket == 0) {
            return;
        }
        //Thread.currentThread() :获取当前线程对象
        //Thread.currentThread().getName() ：获取当前线程对象名称
        System.out.println(Thread.currentThread().getName()+"售出第"+(101-ticket)+"票");
        ticket--;

        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        MyThreand m1 = new MyThreand();

        new Thread(m1).start();
        new Thread(m1).start();
        new Thread(m1).start();
    }
}
